import 'dart:io';

import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:flutter_base/config/app_color.dart';
import 'package:flutter_base/widgets/button/common_button.dart';
import 'package:permission_handler/permission_handler.dart';

class PermissionUtils {
  static Future<bool> checkPermission(Permission permission, {Function? settingPermission}) async {
    if (Platform.isAndroid && permission == Permission.photos) {
      final androidInfo = await DeviceInfoPlugin().androidInfo;
      if (androidInfo.version.sdkInt <= 32) {
        permission = Permission.storage;
      } else {
        permission = Permission.photos;
      }
    }

    PermissionStatus status = await permission.request();
    if (status == PermissionStatus.denied || status == PermissionStatus.permanentlyDenied) {
      String infoStr = '';
      if (permission == Permission.camera) {
        infoStr = "相机";
      } else if (permission == Permission.photos || permission == Permission.storage) {
        infoStr = "相册";
      } else if (permission == Permission.notification) {
        infoStr = "通知";
      } else if (permission == Permission.location) {
        infoStr = "定位";
      }
      var value = await Get.dialog(_PermissionDialog(infoStr));
      if (value == true && GetPlatform.isAndroid) {
        await openAppSettings();
      }
    }
    return status != PermissionStatus.denied && status != PermissionStatus.permanentlyDenied;
  }
}

class _PermissionDialog extends StatelessWidget {
  const _PermissionDialog(this.permissionName);

  final String permissionName;

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Container(
          margin: EdgeInsets.symmetric(horizontal: 27.w),
          padding: EdgeInsets.symmetric(vertical: 24.w, horizontal: 16.w),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(16.w),
            color: Colors.white,
          ),
          child: Column(
            children: [
              Text(
                "提示",
                style: TextStyle(color: AppColor.color181818, fontSize: 22.sp, fontWeight: FontWeight.w600),
              ),
              8.verticalSpace,
              Text(
                '进入“设置”> one video>允许使用$permissionName',
                textAlign: TextAlign.center,
                style: TextStyle(
                    color: AppColor.color181818.withOpacity(0.8), fontSize: 14.sp, fontWeight: FontWeight.w400),
              ),
              32.verticalSpace,
              CommonButton.expend(
                  title: "设置",
                  type: ButtonType.primary,
                  onClick: () {
                    Get.back(result: true);
                  }),
              12.verticalSpace,
              InkWell(
                onTap: () {
                  Get.back();
                },
                child: Container(
                  width: double.infinity,
                  height: 48.w,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(24.w),
                      border: Border.all(width: 2, color: AppColor.color1B1B1B)),
                  child: Text(
                    "取消",
                    style: TextStyle(color: AppColor.color1B1B1B, fontSize: 16.sp, fontWeight: FontWeight.w600),
                  ),
                ),
              )
            ],
          ),
        ),
      ],
    );
  }



}
